package com.xkstream.client.presenter;

import android.util.Log;

import com.xkstream.client.api.ChatroomApi;
import com.xkstream.client.api.ChatroomService;
import com.xkstream.client.api.LiveApi;
import com.xkstream.client.api.LiveService;
import com.xkstream.client.base.BaseData;
import com.xkstream.client.base.BasePresenterImpl;
import com.xkstream.client.entity.AnchorInfo;
import com.xkstream.client.entity.Game;
import com.xkstream.client.entity.ReceiveGift;
import com.xkstream.client.entity.kt.GiftRankingData;
import com.xkstream.client.rx.RxHelper;
import com.xkstream.client.rx.RxObserver;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoyi on 2017-9-7.
 */

public class LiveRoomPresenter extends BasePresenterImpl implements LiveRoomContract.Presenter {
    private LiveRoomContract.View view;
    private ChatroomService chatroomService;
    private LiveService liveService;

    public LiveRoomPresenter(LiveRoomContract.View view) {
        this.view = view;
        this.chatroomService = ChatroomApi.getInstance().service;
        this.liveService = LiveApi.getInstance().service;
    }

    @Override
    public void getGiftRank(String anchorCode) {
        view.showLoading();
        Map<String, Object> params = new HashMap<>();
        params.put("anchor_user_code", anchorCode);
        liveService.getGiftRanking(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<GiftRankingData>>(this) {
                    @Override
                    protected void onSuccess(BaseData<GiftRankingData> data) {
                        view.hideLoading();
                        view.showGiftRanking(data);
                    }

                    @Override
                    protected void onFail(String message) {
                        view.hideLoading();
                    }
                });
    }

    @Override
    public void loadTodayGiftRecord(String anchorCode) {
        view.showLoading();
        Map<String, Object> params = new HashMap<>();
        params.put("rec_user_code", anchorCode);
        params.put("page", 1);
        liveService.getGiftRecord(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<List<ReceiveGift>>>(this) {
                    @Override
                    protected void onSuccess(BaseData<List<ReceiveGift>> data) {
                        view.hideLoading();
                        view.showGiftRecord(data);
                    }

                    @Override
                    protected void onFail(String message) {
                        view.hideLoading();
                    }
                });
    }

    @Override
    public void setChatroomAdmin(Map<String, Object> params) {
        view.showLoading();
        chatroomService.setOrRemoveAdmin(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {
                        view.hideLoading();
                        view.showSetAdminResult(baseData);
                    }

                    @Override
                    protected void onFail(String message) {
                        view.hideLoading();
                    }
                });
    }

    @Override
    public void chatroomMute(Map<String, Object> params) {
        view.showLoading();
        chatroomService.temporaryMute(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {
                        view.hideLoading();
                        view.showMuteResult(baseData);
                    }

                    @Override
                    protected void onFail(String message) {
                        view.hideLoading();
                    }
                });
    }

    @Override
    public void anchorOnOff(Map<String, Object> params) {
        int status = (int) params.get("anchor_status");
        liveService.anchorOnOff(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData data) {
                        Log.e("LiveRoomPresenter", "result " + data);
                       // view.showGameData(data.getData(),status);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void getAnchorDetail(Map<String, Object> params) {
        liveService.getAnchorDetail(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<List<AnchorInfo>>>(this) {
                    @Override
                    protected void onSuccess(BaseData<List<AnchorInfo>> listBaseData) {
                        if (!listBaseData.getData().isEmpty()){
                            view.showAnchorDetail(listBaseData.getData().get(0));
                        }
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });

    }
}
